package com.zzzy.cont.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzzy.cont.common.enums.ConNoteTypeEnum;
import com.zzzy.cont.common.enums.ConOperateEnum;
import com.zzzy.cont.dao.entity.ConActivityNote;
import com.zzzy.cont.dao.mapper.ConActivityNoteMapper;
import com.zzzy.framework.common.util.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 合同活动记事 服务实现类
 *
 * @author fc
 * @since 2024-05-27
 */
@Service
public class ConActivityNoteService extends ServiceImpl<ConActivityNoteMapper, ConActivityNote> {

    @Resource
    private ConService conService;

    public void addSysNote(Long conId, ConOperateEnum operateEnum){
        addSysNote(conId, operateEnum.getValue() + "合同");
    }

    public void addSysNote(Long conId, String content){
        conService.validateAuthForQuery(conId);
        ConActivityNote conActivityNote = new ConActivityNote();
        conActivityNote.setConId(conId);
        conActivityNote.setOperatorName(SecurityUtils.getLoginUser().getNickName());
        conActivityNote.setNoteType(ConNoteTypeEnum.SYS.getValue());
        conActivityNote.setContent(content);
        conActivityNote.setCreateTime(LocalDateTime.now());
        baseMapper.insert(conActivityNote);
    }

    public void addUserNote(Long conId, String content){
        conService.validateAuthForQuery(conId);
        ConActivityNote conActivityNote = new ConActivityNote();
        conActivityNote.setConId(conId);
        conActivityNote.setOperatorName(SecurityUtils.getLoginUser().getNickName());
        conActivityNote.setNoteType(ConNoteTypeEnum.USER.getValue());
        conActivityNote.setContent(content);
        conActivityNote.setCreateTime(LocalDateTime.now());
        baseMapper.insert(conActivityNote);
    }

    public List<ConActivityNote> getActivityNoteListByConId(Long conId) {
        conService.validateAuthForQuery(conId);
        return baseMapper.selectList(new LambdaQueryWrapper<ConActivityNote>().eq(ConActivityNote::getConId, conId).orderByAsc(ConActivityNote::getId));
    }

    public void deleteByConId(Long conId) {
        baseMapper.delete(new LambdaQueryWrapper<ConActivityNote>().eq(ConActivityNote::getConId, conId));
    }
}
